package API;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

import static Util.Tools.getMD5;
import static Util.Tools.getURLParameters;

public class PayNotify {
    public static void main(String[] args) throws UnsupportedEncodingException {

        //异步通知的完整url+内容，自己服务器获取到赋值给PayNotify
        String PayNotify ="" +
                "http://www.baidu.com/notify?result=1&pay_message=&agent_id=1664502&jnet_bill_no=H2506095736155A6&agent_bill_id=20250609133846&pay_type=30&pay_amt=0.01&remark=%7b%22msg%22%3a%22%b4%fd%d6%a7%b8%b6%cf%ee%22%2c%22code%22%3a%221930146831722934274%22%7d&pay_user=oE2GLs4GVHxOKzdig3rsCIBp53PM&trade_bill_no=4200002712202506095257017798&deal_time=20250609133916&bank_card_type=-1&bank_card_owner_type=0&sign=b1423f4a467942e0b791ac072ec22bb1";

        String b = URLDecoder.decode(PayNotify,"GBK");

        //支付的key，登录商户后台密钥查询第一行
        String key = "651886F030134F848F548A12";

        Map<String, String> map = getURLParameters(b);

        Object agent_id = map.get("agent_id");
        Object result = map.get("result");
        Object jnet_bill_no = map.get("jnet_bill_no");
        Object agent_bill_id = map.get("agent_bill_id");
        Object pay_type = map.get("pay_type");
        Object pay_amt = map.get("pay_amt");
        Object remark = map.get("remark");
        Object sign = map.get("sign");

        System.out.println("异步通知返回的sign：" + sign);
        System.out.println();


        String Sign = "result="+ result +
                "&agent_id=" + agent_id +
                "&jnet_bill_no=" + jnet_bill_no +
                "&agent_bill_id=" + agent_bill_id +
                "&pay_type=" + pay_type +
                "&pay_amt=" + pay_amt +
                "&remark=" + remark +
                "&key=" + key ;

        System.out.println("根据通知内容组装的待签名串：" + Sign);
        System.out.println();


        String signToMd5 = getMD5(Sign);

        if (sign.equals(signToMd5)) {
            System.out.println("验签验证成功");
        }

    }
}
